package com.fowo.api.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.controller.CommonController;
import com.fowo.api.common.model.R;
import com.fowo.api.common.model.UpdateDisabledModal;
import com.fowo.api.common.util.DataUtils;
import com.fowo.api.sys.entity.SysUser;
import com.fowo.api.sys.model.*;
import com.fowo.api.sys.service.SysUserService;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Tag(name = "系统用户")
@RequestMapping("/api/sys/user")
@RestController
public class SysUserController extends CommonController {

    @Resource
    private SysUserService userService;

    @Operation(summary = "查询不分页")
    @GetMapping()
    public R<List<SysUserItemVo>> search(SysUserSearch search) {
        search.setDisableDataPremissions(true);
        final List<SysUserItemVo> sysUserItemVoPage = userService.search(search);
        DataUtils.desensitization(sysUserItemVoPage);
        return R.data(sysUserItemVoPage);
    }

    // @PreAuthorize("@am.canAccess('sysUser')")
    @Operation(summary = "查询分页")
    @GetMapping("page")
    public R<Page<SysUserItemVo>> page(SysUserSearch search) {
        search.setDisableDataPremissions(true);
        final Page<SysUserItemVo> sysUserItemVoPage = userService.searchPage(search);
        DataUtils.desensitization(sysUserItemVoPage.getRecords());
        return R.data(sysUserItemVoPage);
    }

    @Operation(summary = "获取指定 id 的记录")
    @GetMapping("{id}")
    public R<SysUserVo> getById(@PathVariable Long id) {
        SysUserVo sysUser = userService.getVoById(id);
        if (sysUser != null) {
            DataUtils.desensitization(sysUser);
        }
        return R.data(sysUser);
    }

    @Operation(summary = "获取当前用户的记录")
    @GetMapping("current")
    public R<SysUserVo> getCurrent(JwtUserInfo jwtUserInfo) {
        if (jwtUserInfo != null) {
            return getById(jwtUserInfo.getUserId());
        }
        return R.fail("用户未登录");
    }

    @Operation(summary = "修改当前用户密码")
    @PutMapping("current/password")
    public R changePassword(JwtUserInfo jwtUserInfo, @RequestBody @Validated ChangePassword changePassword) {
        if (jwtUserInfo != null) {
            return R.run(() -> userService.changePassword(jwtUserInfo.getUserId(), changePassword));
        }
        return R.fail("用户未登录");
    }

    @PreAuthorize("@am.canAccess('sysUserCreate')")
    @Operation(summary = "创建")
    @PostMapping
    public R<SysUser> create(@Validated @RequestBody CreateSysUser model) {
        return R.data(() -> userService.create(model));
    }

    @PreAuthorize("@am.canAccess('sysUserEdit,sysUserSetRoles')")
    @Operation(summary = "更新")
    @PutMapping
    public R update(@RequestBody SysUserVo sysUserVo) {
        return R.ok(() -> userService.update(sysUserVo));
    }

    @PreAuthorize("@am.canAccess('sysUserDelete')")
    @Operation(summary = "删除")
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable Long id) {
        return R.run(() -> userService.removeById(id));
    }

    @PreAuthorize("@am.canAccess('sysUserResetPassword')")
    @Operation(summary = "重置密码")
    @PostMapping("{id}/resetPassword")
    public R resetPassword(@PathVariable Long id, @Validated @RequestBody ResetPassword resetPassword) {
        return R.ok(userService.resetPassword(id, resetPassword));
    }

    @PreAuthorize("@am.canAccess('sysUserEdit')")
    @Operation(summary = "更新禁用状态")
    @PutMapping("{id}/disabled")
    public R updateDisabled(@PathVariable Long id, @Validated @RequestBody UpdateDisabledModal updateDisabledModal) {
        return R.ok(() -> userService.updateDisabled(id, updateDisabledModal.getIsDisabled()));
    }
}
